package com.wf.smartcomm.server.serviceImpl;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wf.smartcomm.server.bean.Merchant;
import com.wf.smartcomm.server.bean.Scores;
import com.wf.smartcomm.server.bean.Terminal;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.dao.ScoreDao;
import com.wf.smartcomm.server.model.Page;
import com.wf.smartcomm.server.service.MerchantService;
import com.wf.smartcomm.server.service.MerchantUserService;
import com.wf.smartcomm.server.service.ScoreService;
import com.wf.smartcomm.server.service.TerminalService;
import com.wf.smartcomm.server.service.UserService;

@Service
@Transactional
public class ScoreServiceImpl implements ScoreService {
	
	@Resource
	private SessionFactory sessionFactory;
	
	@Resource
	private MerchantService merchantService;
	
	@Resource
	private MerchantUserService merchantUserService;
	
	@Resource
	private UserService userService;
	
	
	@Resource
	private ScoreDao scoreDao;
	
	@Resource
	private TerminalService terminalService;
	
	@Override
	public void save(Scores scores) throws Exception {
		Session session = sessionFactory.getCurrentSession();
		session.persist(scores);
		User customer = scores.getCustomer();
		customer.setTotalScore(customer.getTotalScore().add(scores.getScores()));
		customer.setTotalAmount(customer.getTotalAmount().add(scores.getAmount()));
		session.merge(customer);
		Merchant merchant = merchantService.get(scores.getMerchant().getObjectId());
		merchant.setTotalScore(merchant.getTotalScore().add(scores.getScores()));
		merchant.setTotalAmount(merchant.getTotalAmount().add(scores.getAmount()));
		session.merge(merchant);
		Terminal terminal = terminalService.get(scores.getTerminal().getObjectId());
		terminal.setTotalScore(terminal.getTotalScore().add(scores.getScores()));
		terminal.setTotalAmount(terminal.getTotalAmount().add(scores.getAmount()));
		terminalService.update(terminal);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void list(Page page) throws Exception {
		Query query = sessionFactory.getCurrentSession().createQuery("select count(*) from Scores");
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = sessionFactory.getCurrentSession().createQuery("from Scores order by createdAt desc");
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void list(User user, Page page) throws Exception {
		Query query = sessionFactory.getCurrentSession().createQuery("select count(*) from Scores where customer =:customer");
		query.setParameter("customer", user);
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = sessionFactory.getCurrentSession().createQuery("from Scores where customer =:customer order by createdAt desc");
		query.setParameter("customer", user);
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Page pageList(User user, Page page) throws Exception {
		return scoreDao.list(user, page);
	}

}
